package com.yaorange.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yaorange.entity.Article;
import com.yaorange.mapper.ApplyMapper;
import com.yaorange.mapper.ArticleMapper;
import com.yaorange.mapper.ArticleTypeMapper;
import com.yaorange.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;

/**
 * @description:
 * @company: yaorange
 * @author: CorJ
 * @version: 1.0
 * @create: 2021-11-29 19:36
 */
@Service
@Transactional
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    ApplyMapper applyMapper;
    @Override
    public PageInfo<Article> getPage(Integer pageNum, Integer pageSize, HashMap hashMap) {
        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<>(articleMapper.selectAll(hashMap));
    }

    @Override
    public int addArticle(Article article) {
         int a = articleMapper.insertArticle(article);
         if(a>0){
             return 1;
         }
        return 0;
    }

    @Override
    public Article getArticle(Integer id) {
        return articleMapper.selectArticleById(id);
    }

    @Override
    public int update(Article article) {
        int a = articleMapper.updateById(article);
        if(a>0){
            return 1;
        }
        return 0;
    }

    @Override
    public boolean delete(Integer id) {
        int x = articleMapper.deleteById(id);
        if(x>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean findNum(Integer num, Integer id) {
      int articleNum = articleMapper.selectNumById(id);
      if(num>articleNum){
          return false;
      }else {
          articleMapper.updateNumById(num,id);
          return true;
      }
    }


}
